Skip to content

[DowngradePhp81] Add AddReturnTypeWillChangeAttributeRector#372

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
jquiaios:add-return-type-will-change-attribute-rector
May 30, 2026
Merged

[DowngradePhp81] Add AddReturnTypeWillChangeAttributeRector#372
TomasVotruba merged 1 commit into
rectorphp:mainfrom
jquiaios:add-return-type-will-change-attribute-rector

Conversation

@jquiaios

@jquiaios jquiaios commented May 8, 2026

Copy link
Copy Markdown
Contributor

Hello there 👋

Here's a proposition to fix this old issue rectorphp/rector#9515

Here's a global explanation of the issue and proposed fix (if necessary):

PHP 8.1 added provisional return types to built-in interfaces (ArrayAccess, Countable, Iterator, IteratorAggregate and Stringable), triggering deprecation notices on implementations without matching return types.

I created a new AddReturnTypeWillChangeAttributeRector rule that automatically detects affected methods, and adds #[\ReturnTypeWillChange] to suppress those notices.

The rule is registered in the downgrade-php81 set so it runs automatically for anyone using ->withDowngradeSets(php81: true).

The rule is covered by unit tests, including fixtures for the positive cases (ArrayAccess, Countable, Iterator, IteratorAggregate and Stringable), and skip cases (method already has a return type, attribute already present).

- PHP 8.1 added provisional return types to built-in interfaces (ArrayAccess, Countable, Iterator, IteratorAggregate, Stringable), triggering deprecation notices on implementations without matching return types.
- This rule automatically adds #[\ReturnTypeWillChange] to affected methods, suppressing those notices for codebases that need to remain compatible with older PHP versions and cannot yet add the required return types.

Fixes: rectorphp/rector#9515
@TomasVotruba TomasVotruba requested a review from samsonasik May 26, 2026 08:14
@TomasVotruba

Copy link
Copy Markdown
Member

Hi @jquiaios , thanks for the fix 👍 Let's ship it and see how it performs on Rector codebase downgrade

@TomasVotruba TomasVotruba merged commit cbb17de into rectorphp:main May 30, 2026
8 checks passed
@TobiasBg

Copy link
Copy Markdown
Contributor

Hi @jquiaios @samsonasik !

Thanks for working on this!

Would it be possible to also extend this to JsonSerializable/jsonSerialize, see the original example of the issue?

Should I open a new issue for that?

Best wishes,
Tobias

@TomasVotruba

Copy link
Copy Markdown
Member

Hey, sure. Can you send PR with the change instead? It would be faster for us to ship

@TobiasBg

Copy link
Copy Markdown
Contributor

Hi @TomasVotruba!

Sure! I created #380 with JsonSerializable added to the mapping and a test.
(I'm not sure how to test this PR though, sorry.)

Best wishes,
Tobias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants